<?php

namespace App\Exception\Handler;

use App\Exception\LogicException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
 * 将逻辑异常转换为标准异常
 */
class LogicExceptionHandler extends ExceptionHandler
{
    /**
     * @var StdoutLoggerInterface
     */
    protected $logger;

    public function __construct(StdoutLoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function handle(\Throwable $throwable, ResponseInterface $response)
    {
        $this->stopPropagation();
        if(env("APP_ENV") !== "prod"){
            $this->logger->notice("业务异常：".$throwable->getMessage());
        }
        return $response;
    }

    public function isValid(Throwable $throwable): bool
    {
        return $throwable instanceof LogicException;
    }
}